package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "部门管理")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

//    @GetMapping
//    public AjaxResult list() {
//        List<Department> list = departmentService.queryAll();
//        return AjaxResult.ok(list);
//    }

    //    @GetMapping({"/{pageIndex}/{pageSize}/{keyword}",
//                "/{pageIndex}/{pageSize}/{keyword}/{managerId}",
//                "/{pageIndex}/{pageSize}/{keyword}/{managerId}/{parentId}",
//                "/{pageIndex}/{pageSize}"})
    @GetMapping
    public AjaxResult list(DepartmentQuery query) {
        PageList<Department> list = departmentService.queryPage(query);
        return AjaxResult.ok(list);
    }

    @GetMapping("/superDept")
    public AjaxResult superDept() {
        List<Department> list = departmentService.getSuperDept();
        return AjaxResult.ok(list);
    }

    @GetMapping("/parentList")
    public AjaxResult getParentList() {
        List<Department> list = departmentService.getParentList();
        return AjaxResult.ok(list);
    }

    @PostMapping
    public AjaxResult save(@RequestBody Department department) {

        boolean result = false;
        try {
            result = departmentService.save(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.toAjax(result);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        boolean result = departmentService.delete(id);
        return AjaxResult.toAjax(result);
    }

    @DeleteMapping
    public AjaxResult batchDelete(@RequestBody Long[] ids){
        boolean result = departmentService.batchDelete(ids);
        return AjaxResult.toAjax(result);
    }
}
